package com.mckillion.icare;
//The purpose of this class is to show the inactivity screen, this can be used to turn the monitor on or off.

//Import the required java packages.
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ToggleButton;
import android.widget.TextView;
import com.mckillion.icare.R;

public class InactivityScreen extends Activity{
	
	//Declare the variables that will be used within the class.
	private ToggleButton statusButton;
	private ImageView infoButton;
	private TextView infoText;
	private ImageButton backButton, homeButton, settingsButton;
	private boolean checkPref;
	private String time;
	private SharedPreferences prefs;
	private TextView timePref;
	
	//Override onCreate so that the code will run when the activity is started.
	@Override
    public void onCreate(Bundle savedInstanceState) {
        //Call to the super class.
		super.onCreate(savedInstanceState);
        
		 //Remove title and notification bars to give it an android feel.
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
		//Set the content layout to the inactivity_screen XML file.
		setContentView(R.layout.inactivity_screen);
               
        //Set the declared objects to match the objects in the XML file.
        statusButton = (ToggleButton) findViewById(R.id.toggleButton1);
        timePref = (TextView)findViewById(R.id.textView3);
        
        backButton = (ImageButton) findViewById(R.id.back_button);
        homeButton = (ImageButton) findViewById(R.id.home_button);
        settingsButton = (ImageButton) findViewById(R.id.settings_button);
        
    	infoButton=(ImageView)findViewById(R.id.imageView4);
    	
        //Set a listener for the information button
        infoButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//Use a layout inflater to open a pop-up window with the informational text.
				LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
				View popupView = layoutInflater.inflate(R.layout.info_popup, null);  
			    final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT);
			    
			    infoText = (TextView) popupWindow.getContentView().findViewById(R.id.info_text);
			    infoText.setText("This is the inactivity alerts screen." + "\n" + "\n" +
			    					"To enable/disable the service use the toggle button." + "\n" + "\n" +
			    					"The current inactivity time measure is shown, to change this navigate to the iCare settings."
			    					+ "\n" + "\n" + "Press dismiss to close this message.");
			    
			    //create a dismiss button to close the pop-up window.
			    Button dismiss = (Button)popupView.findViewById(R.id.dismiss_button);
	            dismiss.setOnClickListener(new Button.OnClickListener(){
	            	@Override
	            	public void onClick(View v) {
	            		popupWindow.dismiss();
	            	}});
	               
	            //Put the pop-up window in the centre of the screen
	            popupWindow.showAtLocation(infoButton, Gravity.CENTER, 0, 0);
	   }});
        
        //Set a listener for the home button which starts the HomeScreen class
        homeButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(InactivityScreen.this, HomeScreen.class);
				startActivity(i);
			}
		});
        
        //Set a listener for the back button which starts the CalenderScreen class
        backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(InactivityScreen.this, CalenderScreen.class);
				startActivity(i);
			}
		});
        
        //Set a listener for the settings button which starts the Settings class
        settingsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(InactivityScreen.this, Settings.class);
				startActivity(i);
			}
		});
        
        //Get preference value from sharedPreferences to show the current time measurement
        prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        time = prefs.getString("inactivity_length", "No Value Entered");
        timePref.setText(time + " minutes");
        
        //Get the current status from shared preferences
        checkPref = prefs.getBoolean("inactivity_pref", true);
        
        //If the status in preferences is "on" change the toggle button.
        if(checkPref == true){
        	statusButton.toggle();
        }
        
        //Set an actionListener for the ToggleButton
        statusButton.setOnClickListener(new Button.OnClickListener(){
    	    public void onClick(View arg0) {
    	    		//If the toggle button is pressed save the change to the shared preferences.
    	    		SharedPreferences.Editor editor = prefs.edit();
    	    		boolean checked = statusButton.isChecked();
                    editor.putBoolean("inactivity_pref", checked);
                    editor.commit();
                }});    		     
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{	//inflate the android option_menu from XML layout
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.option_menu, menu);
	    return true;
	}
	
	public void goHome(){
		//Create a method to handle a button press on the menu
		Intent i = new Intent(this, HomeScreen.class);
		startActivity(i);
	}
	
	public void showSettings(){
		//Create a method to handle a button press on the menu
		Intent i = new Intent(this, Settings.class);
		startActivity(i);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_home:
	            goHome(); //if home button is pressed call the goHome method
	            return true;
	        case R.id.menu_settings:
	            showSettings(); //if settings button is pressed call the goHome method
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	public void onBackPressed() {
		//handle the back button being pressed
		Intent i = new Intent(InactivityScreen.this, CalenderScreen.class);
		startActivity(i);
	}
}